﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLToolkit.DataAccess;
using Platform.Model;
using iTextSharp.text;
using iTextSharp.text.pdf;


namespace PlatformManagement
{
    public partial class StockReportForm : DevComponents.DotNetBar.Metro.MetroForm
    {
        public StockReportForm()
        {
            InitializeComponent();
            dgvReport.AutoGenerateColumns = false;
            
        }

        private void StockReportForm_Load(object sender, EventArgs e)
        {
            dgvReport.DataSource = ViewStock.Objects.Where(x => x.MarketId == WorkSession.Current.Market.ID).ToList();
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                dgvReport.DataSource =
                    ViewStock.Objects.Where(
                        x =>
                        x.StockDate.Day == dtpDate.Value.Day && x.StockDate.Month == dtpDate.Value.Month &&
                        x.StockDate.Year == dtpDate.Value.Year && x.MarketId==WorkSession.Current.Market.ID).ToList();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            
            if (dgvReport.Rows.Count == 0)
                MessageBox.Show("Không có dữ liệu để xuất");
            else
                {

                    ExportToExcel();
                }


        }
        private void ExportToExcel()
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application xlApp;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlApp = new Microsoft.Office.Interop.Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


                // Thay đổi thuộc tính của Workbook 
                xlWorkSheet.Columns.ColumnWidth = 20;

                // Lưu trữ một phần tiêu đề trong Excel
                xlWorkSheet.Cells[1, 6] = "Danh sách hàng tồn ngày: " + dtpDate.Value.ToShortDateString();

                for (int i = 1; i < dgvReport.Columns.Count + 1; i++)
                {

                    xlWorkSheet.Cells[4, i] = dgvReport.Columns[i - 1].HeaderText;
                }

                // Mỗi hàng lưu trữ và giá trị cột excel
                for (int i = 0; i < dgvReport.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgvReport.Columns.Count; j++)
                    {

                        xlWorkSheet.Cells[i + 5, j + 1] = dgvReport.Rows[i].Cells[j].Value.ToString();
                    }
                }
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
               

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        
        
    }
}
